<template>
    <m-navbar :isNeedBack="false" title="作业"/>
    <m-tabbar :activeId="2"/>
    <!-- 无数据 -->
    <template v-if="!homeWorkList.length">
        <view class="w-full h-page-empty flex-justifyCenter-itemsCenter-box">
            <u-empty
                mode="order"
                width="200"
                text="暂无作业哦~"
                icon="https://cdn.uviewui.com/uview/empty/order.png"
            />
        </view>
    </template>
    <!-- 公告列表 -->
    <template v-else>
        <view class="content-wrap tab-page-height box-border">
            <view
                class="flex-col-box box-border w-full mb-12 rounded-6 u-border p-12"
                v-for="item in homeWorkList"
                :key="item.id"
            >
                <view class="w-full pb-14">{{ `${item.day}  ${item.week}` }}</view>
                <view class="w-full divider"></view>
                <view class="w-full flex-itemsCenter-box flex-wrap sub-box mt-14">
                    <view
                        class="custom-style mr-10 mb-8"
                        v-for="subItem in item.subjectNum"
                        :key="subItem.id"
                    >
                        <u-button
                            color="#70B9BE"
                            type="primary"
                            size="small"
                            @click="toPage(subItem)"
                        >
                            {{ subItem.courseName }}
                        </u-button>
                    </view>
                </view>
            </view>
        </view>
    </template>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { PageParams } from '@/api/request'
import { httpHomeWorkList } from '@/api/modules/achievement'
import AnnouncementCard from "@/pages/announcement/component/AnnouncementCard.vue";

const homeWorkList = ref([])

// 分页数据
const pageParams: PageParams = reactive({
	pageNum: 1,
	pageSize: 10,
	status: '0'
})

const toPage = (subItem: any) => {
	uni.setStorageSync('homeWorkDetail', {
        ...subItem,
        // uploadHomeworkName: "曹默耘的语文作业.docx",
        // uploadHomeworkUrl: "http://www.jsjserve.top/prod-api/profile/upload/2024/04/04/NirWFvjqUlcf9a671294d803db8b440182984b5cfa55_20240404002825A012.docx"
    })
	uni.navigateTo({
		url: '/pages/homeWorkDetail/index'
	})
}

// 获取作业列表
const getHomeWorkList = () => {
	httpHomeWorkList(pageParams).then(res => {
        console.log('res', res)
        if (res.code === 200 && res.data) {
            const arr = []
            Object.keys(res.data).forEach(item => {
                const subjectNum = []
                Object.keys(res.data[item]).forEach(subItem => {
                    subjectNum.push(res.data[item][subItem][0])
                })
                arr.push({
                    day: item,
                    week: weekDay(item),
                    subjectNum
                })
            })
            homeWorkList.value = arr
        } else {
            homeWorkList.value = []
        }
	})
}

const  weekDay = (time: string) => {
	const dateList = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
	return dateList[new Date(time).getDay()]
}

onShow(() => {
	getHomeWorkList()
})
</script>

<style scoped lang="scss">
.custom-style {
    width: 142rpx;
}

.sub-box {
    & :nth-child(4n) {
        margin-right: 0;
    }
}

.divider {
    height: 1px;
    background: #dadbde;
}
</style>
